import uuid, re, hashlib
from datetime import datetime, date
from typing import Union

class User(object):
    '''可选关键字参数：
            uuid : UUID(指定用户的uuid);'''
    def __init__(self, name : str, password : str, is_hash : bool = False, **kwarg):
        '''is_hash:password已经是哈希加密后的值
        '''
        self.name = name
        if is_hash == False:
            if re.match(r'^[A-Za-z0-9]{6,18}$', password):
                # 密码长度为6-18位,由数字和大小写字母组成
                hash = hashlib.md5()
                hash.update(password.encode())
                res = hash.hexdigest()
                self.password = res
            else:
                raise ValueError('The password format is incorrect!',0)
        else:
            self.password = password
        
        if 'uuid' in kwarg.keys():
            if re.match(r'^.{8}-.{4}-.{4}-.{4}-.{12}$', str(kwarg['uuid'])):
                # 检查uuid格式是否正确
                self.uuid = kwarg['uuid']
            else:
                raise ValueError('The UUID format is incorrect!',1)
        else:
            self.uuid = uuid.uuid1()

    def __iter__(self):
        #yield self.uuid
        #yield self.name
        #yield self.password
        return iter((self.uuid, self.name, self.password))
    
    @classmethod
    def str_to_hash(cls, string:str) -> str:
        m = hashlib.md5()
        m.update(string.encode())
        return m.hexdigest()




class Task(object):
    '''可选关键字参数：
        uuid : UUID(指定任务的uuid);
        content : str(任务的具体描述);
        create_time : datetime(指定任务的创建时间);
        status : int(指定任务的状态,0为未完成,1为已完成,2为已逾期,默认为0);
        complete_time : datetime(指定完成任务的时间)

    '''
    def __init__(self, title : str, deadline : Union[datetime, date], **kwarg):

        self.title = title
        self.deadline = deadline
        self.finish_time = None
        self.complete_time = None

        if type(deadline) == date:
            # full_day : 全天事件
            self.full_day = True
        else:
            self.full_day = False

        if 'uuid' in kwarg.keys():
            if re.match(r'^.{8}-.{4}-.{4}-.{4}-.{12}$', str(kwarg['uuid'])):
                # 检查uuid格式是否正确
                self.uuid = kwarg['uuid']
            else:
                raise ValueError('The UUID format is incorrect!')
        else:
            self.uuid = uuid.uuid1()

        if 'content' in kwarg.keys():
            # 检查content格式是否正确
            if type(kwarg['content']) == str:
                self.content = kwarg['content']
            else:
                raise ValueError('argument "coontent" must be a string')
        else:
            self.content = ''

        if 'create_time' in kwarg.keys():
            # 检查create_time格式是否正确
            if type(kwarg['create_time']) == datetime:
                self.create_time = kwarg['create_time']
            else:
                raise ValueError('argument "create_time" must be a datetime')
        else:
            self.create_time = datetime.now()

        if 'complete_time' in kwarg.keys():
            # 检查complete_time格式是否正确
            if type(kwarg['complete_time']) == datetime:
                self.complete_time = kwarg['complete_time']
            else:
                raise ValueError('argument "complete_time" must be a datetime')
        else:
            self.complete_time = datetime.now()

        if 'status' in kwarg.keys():
            # 检查status格式是否正确
            if kwarg['status'] in [0,1,2]:
                self.status = kwarg['status']
            else:
                raise ValueError('argument "status" must be a int, 0, 1 or 2')
        else:
            self.status = 0

    def __iter__(self):
        return iter((self.uuid,
                     self.title,
                     self.deadline,
                     self.content,
                     self.status,
                     self.create_time,
                     self.complete_time))






        